package cn.gh.duboo.demo.provider.config;

import cn.gh.duboo.demo.controller.IndexController;
import cn.gh.duboo.demo.model.Blog;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.spring.SpringPlugin;
import com.jfinal.render.ViewType;

/**
 * Created by Administrator on 2015/9/17.
 */
public class DemoProviderConfig extends JFinalConfig {

    @Override
    public void configConstant(Constants constants) {
        loadPropertyFile("duboo_demo_provider_config.txt");
        constants.setDevMode(getPropertyToBoolean("devMode", false));
        constants.setViewType(ViewType.JSP);
    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/", IndexController.class);
    }

    @Override
    public void configPlugin(Plugins plugins) {
        // 配置Druid数据库连接池插件
        DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),
                getProperty("user"), getProperty("password").trim());

        StatFilter stat = new StatFilter();
        stat.setMergeSql(true);
        dp.addFilter(stat);

        WallFilter wall = new WallFilter();
        wall.setDbType(JdbcConstants.MYSQL);

        dp.addFilter(wall);

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);

        arp.setShowSql(getPropertyToBoolean("devMode", false));
        arp.setDevMode(getPropertyToBoolean("devMode", false));
        arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
        arp.setDialect(new MysqlDialect());

        // 配置Spring插件
        SpringPlugin sp = new SpringPlugin();

        // 加入各插件到Config
        plugins.add(dp);
        plugins.add(arp);
        plugins.add(sp);
    }

    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {
        // 声明Druid监控页面URL
        handlers.add(new DruidStatViewHandler("/druid"));
    }

    @Override
    public void afterJFinalStart() {
        System.out.println("Demo provider for Dubbo启动完成");
    }
}
